package fast.mvc;

import indzcraft.loader.Slot;

import java.awt.Color;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

@SuppressWarnings("serial")
public class ItemSlotView extends JPanel {
	
	private Slot slot;
	private JLabel lblName;
	private JButton cmdItem = new JButton();
	private JButton cmdEnchant = new JButton();
	private JButton[] cmdGem = new JButton[3];
	private JButton cmdReforge = new JButton();
	private JLabel lblInfo;
	
	public ItemSlotView(Slot slot){
		this.slot = slot;
		
		this.setLayout(null);
		
		Border b, b1, b2;
		b1 = BorderFactory.createEmptyBorder(1, 1, 1, 1);
		b2 = BorderFactory.createLineBorder(Color.GRAY, 2);
		b = BorderFactory.createCompoundBorder(b1, b2);
		setBorder(b);

		lblName = new JLabel(slot.getName());
		lblName.setBounds(5,0,70,20);
		//add(lblName);
		
		cmdItem.setBounds(0, 0, 54, 54);
		cmdItem.setBorder(new LineBorder(Color.black, 1));
		cmdItem.setFont(new Font("sansserif",Font.PLAIN,10));
		cmdItem.setMargin(new Insets(0, 0, 0, 0));
		cmdItem.setVisible(false);
		add(cmdItem);
				
		cmdEnchant.setBounds(55,0,15,15);
		cmdEnchant.setBorder(new LineBorder(Color.black, 1));
		cmdEnchant.setFont(new Font("sansserif",Font.PLAIN,10));
		cmdEnchant.setMargin(new Insets(0, 0, 0, 0));
		cmdEnchant.setVisible(false);
		add(cmdEnchant);
		
		for(int i = 0;i < cmdGem.length;i++){
			cmdGem[i] = new JButton();
			cmdGem[i].setBounds(71+i*16,0,15,15);
			cmdGem[i].setBorder(new LineBorder(Color.black, 1));
			cmdGem[i].setFont(new Font("sansserif",Font.PLAIN,10));
			cmdGem[i].setMargin(new Insets(0, 0, 0, 0));
			cmdGem[i].setVisible(false);
			add(cmdGem[i]);
		}
		
		cmdReforge.setBounds(55,16,63,15);
		cmdReforge.setBorder(new LineBorder(Color.black, 1));
		cmdReforge.setFont(new Font("sansserif",Font.PLAIN,10));
		cmdReforge.setMargin(new Insets(0, 0, 0, 0));
		cmdReforge.setVisible(false);
		add(cmdReforge);
		
		lblInfo = new JLabel(slot.getName());
		lblInfo.setBounds(55,32,80,20);
		lblInfo.setFont(new Font("sansserif",Font.PLAIN,12));
		lblInfo.setVisible(false);
		add(lblInfo);
	}
	
	public void setIcon(String icon){
		cmdItem.setVisible(true);
		ChangeIconTask task = new ChangeIconTask(cmdItem,icon);
		task.execute();
	}
	
	public String getName(){
		return cmdItem.getName();
	}
	
	public void setItemListener(ActionListener l){
        this.cmdItem.addActionListener(l);
    }
	
	public void setEnchantListener(ActionListener l){
		this.cmdEnchant.addActionListener(l);
	}
	
	public void setGemListener(ActionListener l) {
		this.cmdGem[0].addActionListener(l);
		this.cmdGem[1].addActionListener(l);
		this.cmdGem[2].addActionListener(l);
	}
	
	public void setReforgeListener(ActionListener l){
		this.cmdReforge.addActionListener(l);
	}
	
	public Slot getSlot(){
		return slot;
	}
	
	public JButton[] getGems(){
		return cmdGem;
	}
	
	public JButton getItem(){
		return cmdItem;
	}
	
	public JButton getReforge(){
		return cmdReforge;
	}
	
	public JButton getEnchant(){
		return cmdEnchant;
	}

	public void setEnchant(String name) {
		cmdEnchant.setVisible(true);
		String icon = "No_Enchant";
		if(!name.equals("N"))
			icon = "inv_misc_enchantedscroll";
		ChangeIconTask task = new ChangeIconTask(cmdEnchant,icon,true);
		task.execute();
	}
	
	public void setReforge(String from,String to){
		cmdReforge.setVisible(true);
		String text = from.substring(0, 3).trim() + " -> " + to.substring(0, 3).trim();
		cmdReforge.setText(text);
	}
	
	public void setGemSlot(int slot,String color){
		if(!color.equals("")){
			if(color.equals("META") || color.equals("COGWHEEL") || color.equals("PRISMATIC") || color.equals("HYDRAULIC")){
				ChangeIconTask task = new ChangeIconTask(cmdGem[slot],"Socket_Meta",false,true);
				task.execute();
			}
			else if(color.equals("RED")){
				ChangeIconTask task = new ChangeIconTask(cmdGem[slot],"Socket_Red",false,true);
				task.execute();
			}else if(color.equals("YELLOW")){
				ChangeIconTask task = new ChangeIconTask(cmdGem[slot],"Socket_Yellow",false,true);
				task.execute();
			}
			else if(color.equals("BLUE")){
				ChangeIconTask task = new ChangeIconTask(cmdGem[slot],"Socket_Blue",false,true);
				task.execute();
			}
			cmdGem[slot].setVisible(true);
		}else{
			cmdGem[slot].setVisible(false);
		}
	}
	
	public void setGem(int slot,String icon) {
		cmdGem[slot].setVisible(true);
		ChangeIconTask task = new ChangeIconTask(cmdGem[slot],icon,true);
		task.execute();
	}
	
	public void setInfo(int itemLevel,String set){
		lblInfo.setVisible(true);
		lblInfo.setText(itemLevel + " " + set);
	}
	
	public void setTooltip(String arg){
		cmdItem.setToolTipText("<html><script type=\"text/javascript\" src=\"http://static.wowhead.com/widgets/power.js\"></script>"+arg+"</html>");
	}
	
}
